﻿using BuilderPattern.GameCharacter;
using System;
using System.Configuration;
using System.Reflection;

namespace BuilderPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello BuilderPattern!");

            {
                // demo 举例
                Builder builder = new ConcreteBuilder1();
                Director director = new Director(builder);
                Product product = director.Construct(); //构建复杂对象
                Console.WriteLine($"【复杂对象】=> PartA:{product.PartA},PartB:{product.PartB},PartC:{product.PartC}");
            }

            {
                // 游戏角色结构创建
                //xml配置文件与反射方式扩展
                // 1.读取【App.config】配置文件，key = builder
                string builderType = ConfigurationManager.AppSettings["builder"];
                // 2.反射生成对象
                ActorBuilder ab = (ActorBuilder)Assembly.Load("BuilderPattern").CreateInstance(builderType);
                ActorController ac = new ActorController();
                Actor actor = ac.Construct(ab); // 通过指挥者创建完整的建造者对象

                Console.WriteLine($"{actor.Type}的外观，性别：{actor.Sex},面容：{actor.Face},服装：{actor.Costume},发型：{actor.Hairstyle}");
            }
        }
    }

    #region BuilderPattern-Demo
    /// <summary>
    /// 产品
    /// </summary>
    class Product
    {
        public string PartA { get; set; }
        public string PartB { get; set; }
        public string PartC { get; set; }
    }

    /// <summary>
    /// 构建着 & 抽象类
    /// </summary>
    abstract class Builder
    {
        //创建产品对象
        protected readonly Product product = new Product();

        public abstract void BuildPartA();
        public abstract void BuildPartB();
        public abstract void BuildPartC();

        /// <summary>
        /// 返回产品对象
        /// </summary>
        /// <returns></returns>
        public Product GetResult()
        {
            return product;
        }
    }

    /// <summary>
    /// 具体构建者
    /// </summary>
    class ConcreteBuilder1 : Builder
    {
        public override void BuildPartA()
        {
            product.PartA = "A1";
        }

        public override void BuildPartB()
        {
            product.PartB = "B1";
        }

        public override void BuildPartC()
        {
            product.PartC = "C1";
        }
    }

    /// <summary>
    /// 指挥者
    /// </summary>
    class Director
    {
        private Builder _builder;
        public Director(Builder builder)
        {
            _builder = builder;
        }

        public void SetBuilder(Builder builder)
        {
            _builder = builder;
        }

        /// <summary>
        /// 产品构建与组装方法
        /// </summary>
        /// <returns></returns>
        public Product Construct()
        {
            _builder.BuildPartA();
            _builder.BuildPartB();
            _builder.BuildPartC();

            return _builder.GetResult();
        }
    }
    #endregion

}
